package com.apes.fn.business.fixedLoss.modle;

import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.plugin.soe.annotations.State;
import com.apes.framework.plugin.soe.annotations.States;
import com.apes.framework.plugin.soe.metadata.BusinessAttributeValue;
import com.apes.framework.util.ApesConst;
import com.apes.scm.masterdata.frame.model.Dept;
import com.apes.scm.masterdata.party.model.PartyRole;
import com.apes.scm.sal.saleOrder.SaleOrderItem;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;

/**
 * @ClassName : Maintenance
 * @Description : 三方维修单
 * @Author : ZhengZeXian
 * @Date: 2020年11月18日14:54:54
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = Maintenance.CONST_TABLE_NAME)
public class Maintenance extends Model {

    public final static String CONST_TABLE_NAME = "ser_maintenance";

    @Id
    @Column(name = "id", nullable = false, length = 24)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "三方维修单", padding = 4, prefix = "B%(company)%(yy)%(month)", useDateRange = true)
    @Comment("ID")
    private String id;

    @Comment("方案门店")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "dept_id", referencedColumnName = "id", nullable = false)
    private Dept dept;

    @Column(name = "CAR_NUMBER",length = 32)
    @Comment("车牌号码")
    private String carNumber;  //车牌号码

    @Column(name = "introduce_money", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("介绍费金额")
    private double introduceMoney;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "company_id", referencedColumnName = "id")
    @Comment("公司")
    private PartyRole company;

    @Comment("三方定损点")
    @Column(name = "loss_place", length = 200)
    private String lossPlace;

    @Comment("进场公里数")
    @Column(name = "JCGLS", length = 200)
    private String JCGLS;

    @Comment("备注")
    @Column(name = "remark", length = 4000)
    private String remark;

    @States(name = "单据类型", values = {
            @State(id = "0", name = "定损销售"),
            @State(id = "1", name = "正常销售"),
    })
    @Comment("单据类型")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "business_type", nullable = false, referencedColumnName = "id")
    private BusinessAttributeValue businessType;

    @States(name = "单据状态", values = {
            @State(id = "create", name = "创建"),
            @State(id = "repeal", name = "终止"),
    })
    @Column(name = "state", length = 10, nullable = false)
    private String state;

    @Column(name = "price_total", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("总计")
    private double priceTotal = 0;

    public void computeAmt() {
        priceTotal = 0;
        for (MaintenanceItem item : maintenanceItems) {
            priceTotal += item.getSaleMoney();
        }
    }

    @OneToMany(mappedBy = "maintenance", cascade = CascadeType.ALL, orphanRemoval = true)
    private Set<MaintenanceItem> maintenanceItems = new HashSet(); //明细

    public Set<MaintenanceItem> getMaintenanceItems() {
        for (MaintenanceItem item : maintenanceItems) {
            item.setMaintenance(this);
        }
        return maintenanceItems;
    }
}
